using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using THSLib;
using THSERP.BaseProvider;
using THSERP.BusinessEntities;
namespace THSERP.Modules.Branch
{
    public class BranchEntities : ERPModuleEntities
    {
        #region Declare Constant
        public readonly static String strBRBranchsObjectName = "BRBranchs";

        #endregion

        #region Declare all entities variables
        private BRBranchsInfo _BRBranchsObject;
        #endregion

        #region Public Properties
        public BRBranchsInfo BRBranchsObject
        {
            get
            {
                return _BRBranchsObject;
            }
            set
            {
                _BRBranchsObject = value;
            }
        }
        #endregion

        #region Constructor
        public BranchEntities()
            : base()
        {
            _BRBranchsObject = new BRBranchsInfo();
        }

        #endregion

        #region Init Main Object,Module Objects functions
        public override void InitMainObject()
        {

            MainObject = BRBranchsObject;
        }

        public override void InitModuleObjects()
        {
        }

        public override void InitModuleObjectList()
        {
        }

        public override void InitGridControlInTHSList()
        {
        }

        public override void SetDefaultModuleObjectsList()
        {
            try
            {

            }
            catch (Exception)
            {
                return;
            }
        }

        #endregion

        #region Invalidate Module Objects functions


        public override void InvalidateModuleObjects(int iObjectID)
        {
        }


        #endregion

        #region Save Module Objects functions

        public override void SaveModuleObjects()
        {
        }

        #endregion

        public override void UpdateInventory(ERPModuleItemsEntity entItems, string strUpdateStatus)
        {
            THSDbUtil dbUtil = new THSDbUtil();

            String strMainTableName = THSUtil.GetTableNameFromBusinessObjectType(typeof(BRBranchsInfo));
            String strItemTableName = THSUtil.GetTableNameFromBusinessObjectType(typeof(BRPOSRedemptionItemsInfo));
            BusinessObject objItemsInfo = ((ERPModuleItemsEntity)entItems).SetToBusinessObject(strItemTableName);
            String strItemProductTypeColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductType";
            String strItemProductType = THSDbUtil.GetPropertyValue(objItemsInfo, strItemProductTypeColumnName).ToString();

            //not update inventory if product is Service or Comment

            if (!strItemProductType.Contains(Product.ProductModule.cstProductTypeService) && !strItemProductType.Contains(Product.ProductModule.cstProductTypeComment))
            {
                int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICProductID"));
                int iICStockID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICStockID"));
                int iICStockSlotID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICStockSlotID"));


                String strItemProductQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";

                double dbItemProductQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItemsInfo, strItemProductQtyColumnName));

                String strItemProductSerialNoColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductSerialNo";
                String strItemProductSerialNo = THSDbUtil.GetPropertyValue(objItemsInfo, strItemProductSerialNoColumnName).ToString();

                String strItemStatus = THSDbUtil.GetPropertyValue(objItemsInfo, "AAStatus").ToString();

                if (strUpdateStatus == BusinessObject.DefaultAAStatus)
                {
                    //Update Inventory Stock
                    if (strItemStatus == BusinessObject.DefaultAAStatus)
                    {
                        int iICInventoryStockID = TransactionUtil.UpdateInventoryStock(
                                                    iICProductID, iICStockID,
                                                    dbItemProductQty,
                                                    TransactionUtil.cstInventoryReceipt);

                        //Update Inventory Stock Slot
                        if (iICInventoryStockID > 0)
                        {
                            int iICInventoryStockSlotID = TransactionUtil.UpdateInventoryStockSlot(
                                                            iICInventoryStockID, iICProductID,
                                                            iICStockSlotID,
                                                            dbItemProductQty,
                                                            TransactionUtil.cstInventoryReceipt);
                            int iICInventoryStockSerieID = TransactionUtil.UpdateInventoryStockSerials(
                                                            iICInventoryStockID, iICProductID,
                                                            iICStockID, strItemProductSerialNo,
                                                            dbItemProductQty,
                                                            TransactionUtil.cstInventoryReceipt);
                        }
                    }
                }
                else if (strUpdateStatus == BusinessObject.DeletedAAStatus)
                {
                    //Update Inventory Stock
                    int iICInventoryStockID = TransactionUtil.UpdateInventoryStock(
                                                iICProductID, iICStockID,
                                                -dbItemProductQty,
                                                TransactionUtil.cstInventoryReceipt);

                    //Update Inventory Stock Slot
                    if (iICInventoryStockID > 0)
                    {
                        int iICInventoryStockSlotID = TransactionUtil.UpdateInventoryStockSlot(
                                                        iICInventoryStockID, iICProductID,
                                                        iICStockSlotID,
                                                        -dbItemProductQty,
                                                        TransactionUtil.cstInventoryReceipt);
                        int iICInventoryStockSerieID = TransactionUtil.UpdateInventoryStockSerials(
                                                            iICInventoryStockID, iICProductID,
                                                            iICStockID, strItemProductSerialNo,
                                                            -dbItemProductQty,
                                                            TransactionUtil.cstInventoryReceipt);
                    }
                }

            }
        }
    }
}
